package com.kolon.business.mServiceDesk.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mServiceDesk.controller.MServiceDeskApplyListController")
public class MServiceDeskApplyListController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mServiceDesk/mServiceDeskApplyList.do")
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String page = ComUtil.nullChk(request.getParameter("page"), "1");
		String tid = id;
		//권한을 새션에 호출
		HttpSession session = request.getSession(true);
		String assignee = (String)session.getAttribute("SESSION_ASSIGNEE");
		String approval = (String)session.getAttribute("SESSION_APPROVAL");
		StringBuffer buffer = new StringBuffer();
		buffer.append(url);
		List mapList = new ArrayList();
		Map map = new HashMap();
		// 검색
		buffer.append("/servicedesk.aspx?mode=").append("7");
		buffer.append("&userid=").append(tid);
			// XML DOCUMENT 및 인증 처리
			Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
			Element element = doc.getRootElement();
			List xmlList = element.getChildren("Table");
			Iterator itXml = xmlList.iterator();
			// XML 데이터 
			while (itXml.hasNext()) {
				Element el = (Element)itXml.next();
				map = new HashMap();
				map.put("RN", el.getChild("RN").getText());
				map.put("SEQNO", el.getChild("SEQNO").getText());//승인건 고유번호 (hidden)
				map.put("REQNAME", el.getChild("REQNAME").getText());//요청자(LIST,상세 모두사용)
				map.put("REQNAMEUtf", ComUtil.getEncode(el.getChild("REQNAME").getText(),"UTF-8"));
				map.put("REQDATE", ComUtil.nullChk(el.getChild("REQDATE").getText())); //요청날짜
				map.put("REQDATEUtf", ComUtil.getEncode(el.getChild("REQDATE").getText(),"UTF-8"));
				map.put("DETAIL", ComUtil.subString(el.getChild("DETAIL").getText().replaceAll("\n", ""),15));	//요청내용
				map.put("DETAILUtf", ComUtil.getEncode(el.getChild("DETAIL").getText(),"UTF-8"));
				map.put("REQGUBUN", el.getChild("REQGUBUN").getText());
				//map.put("REQGUBUNUtf", ComUtil.getEncode(el.getChild("REQGUBUN").getText(),"UTF-8"));
				map.put("APPGUBUN", el.getChild("APPGUBUN").getText()); //hidden value
				map.put("APPSTATUS", el.getChild("APPSTATUS").getText()); //hidden value
				mapList.add(map);
			}
		// TableInfo 영역
		Document doc2 = ComUtil.getDocument(buffer.toString(), id, pw);
		Element element2 = doc2.getRootElement();
		List xmlList2 = element2.getChildren("TableInfo");
		Iterator itXml2 = xmlList2.iterator();
		Map map2 = new HashMap();
		// XML 데이터 
		if (itXml2.hasNext()) {
			Element el = (Element)itXml2.next();
			map2 = new HashMap();
			map2.put("TotalRecordCnt", el.getChild("TotalRecordCnt").getText());
			map2.put("TotalPage", el.getChild("TotalPage").getText());
			map2.put("CurrPage", el.getChild("CurrPage").getText());
			map2.put("OnePageCnt", el.getChild("OnePageCnt").getText());
			map2.put("bPublicWrite", el.getChild("bPublicWrite").getText());
			map2.put("UserID", el.getChild("UserID").getText());
			map2.put("SAdminCNT", el.getChild("SAdminCNT").getText());
		}
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("mapList", mapList);
		modelAndView.addObject("mapList2", map2);
		modelAndView.addObject("assignee", assignee);
		modelAndView.addObject("approval", approval);
		modelAndView.addObject("page", page);
		return modelAndView;
	}

}